using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Domain.Entities;
using Domain.Services;
using System.Collections.Generic;

namespace UserInterface.Admin
{
    public partial class EditForum : System.Web.UI.Page
    {
        protected Forum forum;

        protected void Page_Load(object sender, EventArgs e)
        {
            short forumId = short.Parse(Request.QueryString["forumId"]);
            forum = ForumService.GetById(forumId);
            if (forum == null)
            {
                Response.Redirect("ManageForums.aspx");
            }
            if (!IsPostBack)
            {
                NameTextBox.Text = forum.Name;
                Title = (string)GetLocalResourceObject("Title");
                IList<Category> categories = CategoryService.GetAll();
                foreach (Category category in categories)
                {
                    CategoryDropDownList.Items.Add(
                        new ListItem(category.Name, category.Id.ToString())
                    );
                }
                CategoryDropDownList.SelectedValue = forum.Category.Id.ToString();
            }
        }

        protected void OkButton_OnClick(object sender, EventArgs e)
        {
            string name = NameTextBox.Text;
            byte categoryId = byte.Parse(CategoryDropDownList.SelectedValue);
            Category category = CategoryService.GetById(categoryId);
            if (category != null)
            {
                ForumService.UpdateForum(forum, name, category);
            }
            Response.Redirect("ManageForums.aspx");
        }

        protected void CancelButton_OnClick(object sender, EventArgs e)
        {
            Response.Redirect("ManageForums.aspx");
        }
    }
}
